package cnm.storage.impl;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.String;

import cnm.storage.StorageService;
import cnm.storage.UnsupportedStorageObject;
import cnm.model.ResourceTypes;
import cnm.model.ResourceTypesUtil;

public class StorageServiceImpl implements StorageService {

	private String repoDir;
	private String tmpDir;
	
	public void init() {
		File repo = new File(getRepoDir());
		repo.mkdirs();
		File audio = new File(repo, "audio");
		audio.mkdirs();
		File video = new File(repo, "video");
		video.mkdirs();
		File magzine = new File(repo, "magzine");
		magzine.mkdirs();
	}
	
	public void setRepoDir(String repoDir) {
		this.repoDir = repoDir;
	}
	
	public String getRepoDir () {
		return this.repoDir;
	}
	
	public String getTmpDir() {
		return tmpDir;
	}

	public void setTmpDir(String tmpDir) {
		this.tmpDir = tmpDir;
	}

	public boolean loadStream(int type, String uuid, OutputStream outStream)
			throws UnsupportedStorageObject, IOException {
		String fileUri = getRepoDir();
		switch (type) {
			case ResourceTypes.TYPE_AUDIO:
				fileUri += File.separator + "audio"+ File.separator + uuid + "." + ResourceTypesUtil.typeToExt(type);
				break;
			case ResourceTypes.TYPE_VIDEO:
				fileUri += File.separator + "video" + File.separator + uuid + "." + ResourceTypesUtil.typeToExt(type);
				break;
			case ResourceTypes.TYPE_MAGZINE:
				fileUri += File.separator + "magzine" + File.separator + uuid + "." + ResourceTypesUtil.typeToExt(type);
				break;
			default:
				throw new UnsupportedStorageObject(type, "Invalid type");
		}
		
		InputStream in = null;
		try {
			in = new BufferedInputStream(new FileInputStream(new File(fileUri)));
    		byte[] buffer = new byte[8*1024];
    		int length = -1;
    		while ((length=in.read(buffer))>-1) {
    			outStream.write(buffer, 0, length);
    		}	
    		outStream.flush();			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if (null != in) {
				in.close();
			}
		}
		return true;
	}

	public boolean saveStream(int type, String uuid, InputStream inStream)
			throws UnsupportedStorageObject, IOException {
		String fileUri = getRepoDir();
		switch (type) {
			case ResourceTypes.TYPE_AUDIO:
				fileUri += File.separator + "audio"+ File.separator + uuid + "." + ResourceTypesUtil.typeToExt(type);
				break;
			case ResourceTypes.TYPE_VIDEO:
				fileUri += File.separator + "video" + File.separator + uuid + "." + ResourceTypesUtil.typeToExt(type);
				break;
			case ResourceTypes.TYPE_MAGZINE:
				fileUri += File.separator + "magzine" + File.separator + uuid + "." + ResourceTypesUtil.typeToExt(type);
				break;
			default:
				throw new UnsupportedStorageObject(type, "Invalid type");
		}
		
		BufferedInputStream in = null;
		BufferedOutputStream out = null;
		try {
			File file = new File(fileUri);	
    		in = new BufferedInputStream(inStream);
    		out = new BufferedOutputStream(new FileOutputStream(file));
    		byte[] buffer = new byte[8*1024];
    		int length = -1;
    		while ((length=in.read(buffer))>-1) {
    			out.write(buffer, 0, length);
    		}	
    		out.flush();
		}
		catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		finally {
			if (null!=out) {
				out.close();
			}		
		}
		
		return true;
	}

	public boolean persistTmpObject(int type, String tmpUuid, String persistUuid)
			throws UnsupportedStorageObject, IOException {
		// TODO Auto-generated method stub
		/*String tmpFileUri = getTmpDir() + File.separator + "audio" + 
			File.separator + tmpUuid + ".mp3";
		String persistFileUri = getRepoDir() + File.separator + "audio" + 
			File.separator + persistUuid + ".mp3";
		
		InputStream in = null;
		OutputStream out = null;		
		try {
			File tmpFile = new File(tmpFileUri);
			if (!tmpFile.exists()) {
				return false;
			} else {
				in = new BufferedInputStream(new FileInputStream(tmpFile));
				out = new BufferedOutputStream(new FileOutputStream(new File(persistFileUri)));
	    		byte[] buffer = new byte[8*1024];
	    		int length = -1;
	    		while ((length=in.read(buffer))>-1) {
	    			out.write(buffer, 0, length);
	    		}	
	    		out.flush();						
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			return false;
		}
		finally {
			if (null!=in) {
				in.close();
			}
			if (null!=out) {
				out.close();
			}							
		}*/
		
		return true;
	}

}
